package com.ylsf.blog.service.impl;

import com.ylsf.blog.mapper.ArticleMapper;
import com.ylsf.blog.mapper.CommentMapper;
import com.ylsf.blog.pojo.Article;
import com.ylsf.blog.pojo.ArticleComment;
import com.ylsf.blog.pojo.Comment;
import com.ylsf.blog.service.SquareService;
import com.ylsf.blog.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * 博客广场服务实现类
 */
public class SquareServiceImpl implements SquareService {
    @Override
    public List<ArticleComment> listAllArticleComments() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        ArticleMapper articleMapper = sqlSession.getMapper(ArticleMapper.class);
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        List<Article> articles = articleMapper.selectAllArticles();
        List<ArticleComment> articleComments=new ArrayList<>();
        for(Article article:articles){
            List<Comment> comments = commentMapper.selectCommentsArticle(article.getId());
            //Integer id, String title, String content, Date publishedTime, String author, List<Comment> comments
            ArticleComment articleComment=new ArticleComment(article.getId(),article.getTitle(),article.getContent(),article.getPublishedTime(),article.getAuthor(),comments);
            articleComments.add(articleComment);
        }
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
        return articleComments;
    }
}
